package test.driver;

import org.json.JSONObject;
import test.utils.JsonResult;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class HttpAPI {

	private HashMap<String, String> file;
	private HashMap<String, String> postData;
	private String cookie;
	private Map<String, String> myheaders;  //个性化添加的header
	private String header;
	public JSONObject headers;
	private String body;
	private static boolean isProxy = false;  //默认不使用
	private static String proxyUrl;
	private static int proxyPort;
	private boolean isURLModifed = false;  //是否已经修改了url
	private String host = "";
	private String referer = "";
	private String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134";
	private int responseHttpCode;
	private String responseStr;
	private String url;
	private String contentType = "application/json;charset=utf-8";
	private String requestMethod;

	public static int getProxy_port() {
		return proxyPort;
	}

	public static void setProxy_port(int proxy_Port) {
		proxyPort = proxy_Port;
	}

	public static String getProxy_url() {
		return proxyUrl;
	}

	public static void setProxy_url(String proxy_Url) {
		proxyUrl = proxy_Url;
	}
	
	public static boolean getIs_proxy() {
		return isProxy;
	}
	
	public static void setIs_proxy(boolean is_Proxy) {
		isProxy = is_Proxy;
	}
	
	public String getResponseStr() {
		return responseStr;
	}

	public void setResponseStr(String responseStr) {
		this.responseStr = responseStr;
	}

	public int getResponseHttpCode() {
		return responseHttpCode;
	}

	public void setResponseHttpCode(int responseHttpCode) {
		this.responseHttpCode = responseHttpCode;
	}

	public HttpAPI() {
		 file = new HashMap<String , String>();  //用于上传文件
		 postData = new HashMap<String, String>();  //请求接口的数据
		 myheaders = new HashMap<>();
		 cookie = "";
		 header = "";
		 if (isProxy) {
			 setProxy(getIs_proxy(), getProxy_url(),getProxy_port());
		 }
	}
	
	//添加请求数据方法
	public void setParam(String key, String value) {
		if (postData == null) {
			postData = new HashMap<String, String>();
		}
		postData.put(key, value);
	}
	
	public void setCookie(String cookies) {
		cookie = cookies;
	}
	
	public String getCookie() {
		return cookie;
	}
	
	//添加文件上传方法
	public void setUploadFile(String key, String value) {
		if (file == null) {
			file = new HashMap<String , String>();
		}
		file.put(key, value);
	}
	
	/**
	 * get方法
	 * @param api 不包括前缀
	 * @return 请求返回值
	 */
	public JsonResult get() throws Exception {
		MyHttp http = new MyHttp();
		setUrl(url);
		http.seturl(url);
		host = getHost(url);
		setMyheaders("User-Agent", userAgent);
		if (!host.equals("")) {
			setMyheaders("Host", host);
		}
		if (!referer.equals("")) {
			setMyheaders("Referer", referer);
		}
		if (cookie != null && !cookie.equals("")) {
			http.setCookie(cookie);
		}
		if (myheaders != null && myheaders.size() > 0) {
			http.setMyHeaders(myheaders);
		}
		http.SendGetRequest(contentType);
		setResponseHttpCode(http.getResponseStatusCode());
		setResponseStr(http.getResponse());
		header = http.getHeader();
		headers = http.headersObject;
		http.setProxy(isProxy, proxyUrl, proxyPort);
		if (http.getCookie() != null && !"".equals(http.getCookie())) {
			setCookie(http.getCookie());
		}
		if (postData != null) {
			postData.clear();
		}
		return JsonResult.parse(http.getResponse());
	}
	
	/**
	 * post方法
	 * @param api 不包括前缀
	 * @return 请求返回值
	 * @throws Exception 
	 */
	public JsonResult post() throws Exception {
		MyHttp http = new MyHttp();
		http.seturl(url);
		host = getHost(url);
		http.setparam(postData);
		http.setBody(body);
		setMyheaders("User-Agent", userAgent);
		if (!host.equals("")) {
			setMyheaders("Host", host);
		}
		if (!referer.equals("")) {
			setMyheaders("Referer", referer);
		}
		if (cookie != null && !cookie.equals("")) {
			http.setCookie(cookie);
		}
		if (myheaders != null && myheaders.size() > 0) {
			http.setMyHeaders(myheaders);
		}
		http.setProxy(isProxy, proxyUrl, proxyPort);
		if (file != null && file.size() > 0) {
			for (Entry<String, String> entry : file.entrySet()) {
				http.setfile(entry.getKey(), entry.getValue());
			}
			http.postWithFile(contentType);
		}else{
			http.post(contentType);
		}
		setResponseHttpCode(http.getResponseStatusCode());
		setResponseStr(http.getResponse());
		if (postData != null) {
			postData.clear();
		}
		body = "";
		header = http.getHeader();
		headers = http.headersObject;
		if (http.getCookie() != null && !"".equals(http.getCookie())) {
			setCookie(http.getCookie());
		}
		return JsonResult.parse(http.getResponse());
	}
	
	public JsonResult Request() throws Exception {
		if (requestMethod == null) {
			return null;
		}else if (requestMethod.equals("get")) {
			return get();
		}else {
			return post();
		}
	}
	
	public String getHeader() {
		return header;
	}

	public void setBody(String bodystr) {
		body = bodystr;
	}
	
	public String getBody() {
		return body;
	}
	
	public void setProxy(boolean isproxy, String proxyurl, int proxyport) {
		isProxy = isproxy;
		proxyUrl = proxyurl;
		proxyPort = proxyport;
	}

	public void setMyheaders(String key, String value) {
		if (key.equals("Cookie")) {
			cookie = null;
		}
		if (myheaders.containsKey(key)) {
			removeMyHeadr(key);
		}
		myheaders.put(key, value);
	}
	
	public void removeMyHeadr(String key) {
		myheaders.remove(key);
	}

	public boolean isURLModifed() {
		return isURLModifed;
	}

	public void setURLModifed(boolean isURLModifed) {
		this.isURLModifed = isURLModifed;
	}
	
	public String getMyheader() {
		return myheaders.toString();
	}
	
	public String getMyheader(String key) {
		if (myheaders.containsKey(key)) {
			return myheaders.get(key);
		}
		return null;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public String getHost() {
		return host;
	}
	
	private String getHost(String url) {
		host = url.replace("http://", "").replace("https://", "");
		if (host.contains("/")) {
			host = host.split("/")[0];
		}
		if (host.contains(":")) {
			host = host.split(":")[0];
		}
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getReferer() {
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}
}
